package com.gugee.ins.common.dal.service.blogger.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gugee.ins.common.dal.mapper.blogger.InsRelBloggerLocationMapper;
import com.gugee.ins.common.dal.service.blogger.InsRelBloggerLocationDao;
import com.gugee.ins.common.model.mysql.blogger.InsRelBloggerLocation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;



/**
 * InsRelBloggerFollowingDao
 * @author Huangxin
 */
@Slf4j
@Service
public class InsRelBloggerLocationDaoImpl extends ServiceImpl<InsRelBloggerLocationMapper, InsRelBloggerLocation> implements InsRelBloggerLocationDao {


    /**
     * 批量插入 博主和粉丝的关联信息
     * @param insRelBloggerLocations
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchInsert(List<InsRelBloggerLocation> insRelBloggerLocations) {
        try{
            this.saveBatch(insRelBloggerLocations,insRelBloggerLocations.size() + 1);
        }catch (Exception e){
            log.debug("batch insert InsRelBloggerLocation failed ,try insert one by one, msg : {} ",e.getMessage());
            insRelBloggerLocations.forEach(insRelBloggerLocation -> {
                try{
                    baseMapper.insert(insRelBloggerLocation);
                }catch (Exception ex){
                    log.info("insert InsRelBloggerLocation failed ,uid : {} ,locationName : {}, msg : {} "
                            ,insRelBloggerLocation.getUid(),insRelBloggerLocation.getLocationName(),ex.getMessage());
                }
            });
        }
    }


    /**
     * 从帖子表插入
     * @param uid
     * @param timeLimit
     * @return
     */
    @Override
    public void insertByUidTimeLimit(long uid, long timeLimit) {
        baseMapper.insertByUidTimeLimit(uid,timeLimit);
    }

    /**
     * 根据uid查找数据
     * @param uid
     * @return
     */
    @Override
    public List<InsRelBloggerLocation> findByUid(long uid) {
        QueryWrapper<InsRelBloggerLocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        return baseMapper.selectList(queryWrapper);
    }
}
